package cn.lblbc.webbrowser.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.lblbc.webbrowser.R
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import java.io.File

data class BackgroundImageItem(
    val id: String,
    val title: String,
    val subtitle: String,
    val imagePath: String?, // null表示默认背景
    val type: BackgroundImageType
)

enum class BackgroundImageType {
    DEFAULT,
    BUNDLED,
    COLLECTED,
    CUSTOM
}

class BackgroundImageAdapter(
    private val context: Context,
    private val items: List<BackgroundImageItem>,
    private val currentSelection: String?,
    private val onItemClick: (BackgroundImageItem) -> Unit
) : RecyclerView.Adapter<BackgroundImageAdapter.ViewHolder>() {

    private var selectedPosition = items.indexOfFirst { it.imagePath == currentSelection }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val previewImageView: ImageView = view.findViewById(R.id.previewImageView)
        val defaultIconImageView: ImageView = view.findViewById(R.id.defaultIconImageView)
        val titleTextView: TextView = view.findViewById(R.id.titleTextView)
        val subtitleTextView: TextView = view.findViewById(R.id.subtitleTextView)
        val radioButton: RadioButton = view.findViewById(R.id.radioButton)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_background_image, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        
        holder.titleTextView.text = item.title
        holder.subtitleTextView.text = item.subtitle
        holder.radioButton.isChecked = position == selectedPosition

        when (item.type) {
            BackgroundImageType.DEFAULT -> {
                // 显示默认背景图标
                holder.previewImageView.setImageDrawable(null)
                holder.previewImageView.setBackgroundColor(
                    context.resources.getColor(android.R.color.darker_gray, null)
                )
                holder.defaultIconImageView.visibility = View.VISIBLE
            }
            BackgroundImageType.BUNDLED -> {
                // 显示内置drawable资源
                holder.defaultIconImageView.visibility = View.GONE
                try {
                    val resourceId = context.resources.getIdentifier(
                        item.imagePath, 
                        "drawable", 
                        context.packageName
                    )
                    if (resourceId != 0) {
                        Glide.with(context)
                            .load(resourceId)
                            .centerCrop()
                            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                            .into(holder.previewImageView)
                    } else {
                        holder.previewImageView.setImageDrawable(null)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    holder.previewImageView.setImageDrawable(null)
                }
            }
            BackgroundImageType.COLLECTED, BackgroundImageType.CUSTOM -> {
                // 显示外部文件
                holder.defaultIconImageView.visibility = View.GONE
                val file = File(item.imagePath ?: "")
                if (file.exists()) {
                    Glide.with(context)
                        .load(file)
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.DATA)
                        .into(holder.previewImageView)
                } else {
                    holder.previewImageView.setImageDrawable(null)
                }
            }
        }

        holder.itemView.setOnClickListener {
            val oldPosition = selectedPosition
            selectedPosition = position
            notifyItemChanged(oldPosition)
            notifyItemChanged(selectedPosition)
            onItemClick(item)
        }
    }

    override fun getItemCount() = items.size
}